#ifndef _THREADPOOL_H_
#define _THREADPOOL_H_
#include"thread.h"
#include"task.h"
#include<vector>
#include<unordered_map>
#include<queue>
#include<memory>
#include<atomic>
#include<mutex>
#include<thread>
#include<condition_variable>
/*
  example
   ThreadPool pool;
   pool.start(4);
   class MyTask:public Task
   {
     public:
       void run(){ //重写}
   };
*/

enum class POOLMODE //使用enum class ，防止使用枚举时与其它枚举项中的枚举冲突
{
   MODE_FIXED,
   MODE_CACHEd
};
class ThreadPool
{
private:
   std::unordered_map<int,std::unique_ptr<Thread>> threadPools_;
   size_t inintSizeOfPool_ ;
   size_t threadSizeThresHold_;//线程能创建最大数量
   std::atomic_int curThreadSize_;//当前线程数量,因为vector的size是线程不安全的，所以需要这个函数
   std::atomic_int idleThreadSize_;//空闲线程数量

   std::queue<std::shared_ptr<Task>> taskQues_;
   std::atomic_int taskSize_; //多个线程同时从线程中取任务，同时要对这个数量进行修改，所以它为atomic
   size_t taskQueMaxThreshHold_;//任务队列最大值
   
   std::mutex taskQueMutx_;
   std::condition_variable notFull_;
   std::condition_variable notEmpty_;

   POOLMODE poolMode_;
   std::atomic_bool isPoolRunning_;
private:
     void ThreadFunc(int threadId); 
public:
  ThreadPool();
  ~ThreadPool();
  ThreadPool(const ThreadPool&) =delete;
  ThreadPool & operator=(const ThreadPool&)=delete;
  void Start(size_t inintSizeOfPool = std::thread::hardware_concurrency());
  void SetMode(POOLMODE mode = POOLMODE::MODE_FIXED);
  void SetTaskMaxThreshHold(size_t ThreshHold);
  void SetThreadMaxThreshHold(size_t threshHold);
  Result SubmitTask(std::shared_ptr<Task> sp);
};
#endif 